using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Deepcode.Flux.Core.Services.Security;
using Deepcode.Flux.Core.Systems.Security;
using Deepcode.Flux.Core.Extensions;
using Deepcode.Flux.Core.Systems.UI.Taskpad;
using Deepcode.Flux.Core.Systems.Registry;

public partial class Private_My_Profile_ShowProfileWindows : Deepcode.Flux.Core.UI.AdministrationModulePage
{
	protected void Page_Load(object sender, EventArgs e)
	{
		labUpdated.Visible = false;

		if (!Page.IsPostBack)
		{
			// Set the regex validator for the email address
			EmailRE.ValidationExpression = Deepcode.Flux.Core.Services.Assistant.Validations.EmailValidationExpression;
			
			// Load data
			AdministrationSecuritySession u = AdministrationSecurity.GetSession();
			Identity.Text = u.NT_Username;
			RealName.Text = u.RealName;
			EmailAddress.Text = u.Email;

			PopulateTaskpadTree();
		}
	}

	protected override void OnInit(EventArgs e)
	{
		base.OnInit(e);
		EmailExists.ServerValidate += new ServerValidateEventHandler(ValidateEmailExists);
	}
	
	#region Validation
	void ValidateEmailExists(object source, ServerValidateEventArgs args)
	{
		AdministrationUsers Users = new AdministrationUsers();
		AdministrationUser EmailBelongsTo = Users.GetUserByEmail(args.Value);
		AdministrationSecuritySession LoggedInUser = AdministrationSecurity.GetSession();
		if ((EmailBelongsTo != null) && (EmailBelongsTo.PK_ID != LoggedInUser.PK_ID)) args.IsValid = false;
		else args.IsValid = true;
	}
	#endregion
	#region Updates / save
	protected void btnUpdateClick(object sender, EventArgs e)
	{
		if( ! Page.IsValid ) return;

		AdministrationSecuritySession UserSession = AdministrationSecurity.GetSession();
		AdministrationUsers Users = new AdministrationUsers();
		AdministrationUser  u = Users.GetUserByID( UserSession.PK_ID );

		u.EMailAddress = EmailAddress.Text;
		u.RealName = RealName.Text;
		Users.Save( u );

		SaveTaskpad();

		UserSession.Email = u.EMailAddress;
		UserSession.RealName = u.RealName;
		labUpdated.Visible = true;
	}

	protected void SaveTaskpad()
	{
		FluxRegistry RegistryRemoved = new FluxRegistry(FluxRegistryRoot.CURRENT_USER, "/Taskpad/Removed");
		foreach (TreeNode n in _taskpadtree.Nodes)
		{
			foreach (TreeNode n2 in n.ChildNodes)
			{
				if (n2.Checked)
				{
					if (!RegistryRemoved.GetKey(n2.Value, false))
						RegistryRemoved.SetKey(n2.Value, true);
				}
				else
				{
					if (RegistryRemoved.GetKey(n2.Value, false))
						RegistryRemoved.DeleteKey(n2.Value);
				}
			}
		}
	}
	#endregion
	#region Taskpad tree population
	protected void PopulateTaskpadTree()
	{
		// Prepare the registry holding those taskpad items that have been removed
		FluxRegistry RegistryRemoved = new FluxRegistry(FluxRegistryRoot.CURRENT_USER, "/Taskpad/Removed");
		// Clear the treeview nodes
		_taskpadtree.Nodes.Clear();
		
		// Get tabs from addin tree
		AddinTreeNode root = AddinTree.Tree.GetTreeNodeNoThrow("/Flux/Taskpad/");
		if (root != null)
		{
			// Get all of the taskpad tabs
			TaskpadTab[] tabs = root.BuildChildItems(this).ToArray(typeof(TaskpadTab)) as TaskpadTab[];

			// Create each one in the tree
			foreach (TaskpadTab tab in tabs)
			{
				// Create the parent (tab) node
				TreeNode n = new TreeNode(tab.Title, tab.ID);
				n.ShowCheckBox = false;
				n.SelectAction = TreeNodeSelectAction.None;
				_taskpadtree.Nodes.Add( n );
				
				// Create each individual control node - checking for those removed already
				foreach (TaskpadControl c in tab.Options)
				{
					TreeNode n2 = new TreeNode( c.Title, c.ID );
					n2.ShowCheckBox = true;
					n2.SelectAction = TreeNodeSelectAction.None;
					if(RegistryRemoved.GetKey(c.ID, false))
						n2.Checked = true;


					n.ChildNodes.Add( n2 );
				}
			}
		}
	}
	#endregion
}
